﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using vITs.Site.Models.DataModels;

namespace vITs.Site.Models
{
    public class VitsRoles : RoleProvider
    {
        #region Unused

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        #endregion

        public override string[] GetAllRoles()
        {
            var roles = new VitsDB().Roles.ToArray();
            var retval = new string[roles.Length];

            for (int i = 0; i < roles.Length; i++)
            {
                retval[i] = roles[i].Name;
            }

            return retval;
        }

        public override string[] GetRolesForUser(string username)
        {
            VitsDB db = new VitsDB();
            var user = db.Users.SingleOrDefault(u => u.EmailAddress == username);
            if (user != null)
            {
                var roles = user.Roles.ToArray();
                string[] retval = new string[roles.Length];

                for (int i = 0; i < roles.Length; i++)
                {
                    retval[i] = roles[i].Name;
                }

                return retval;
            }

            return new string[0];
        }

        public override string[] GetUsersInRole(string roleName)
        {
            VitsDB db = new VitsDB();
            var role = db.Roles.SingleOrDefault(r => r.Name == roleName);
            if (role != null)
            {
                var users = role.Users.ToArray();
                var retval = new string[users.Length];

                for (int i = 0; i < users.Length; i++)
                {
                    retval[i] = users[i].EmailAddress;
                }

                return retval;
            }

            return new string[0];
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            VitsDB db = new VitsDB();
            var user = db.Users.SingleOrDefault(u => u.EmailAddress == username);
            if (user != null)
            {
                foreach (var role in user.Roles)
                {
                    if (role.Name == roleName) return true;
                }
            }

            return false;
        }

        #region Unused

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        #endregion

        public override bool RoleExists(string roleName)
        {
            return ((from Role r in new VitsDB().Roles where r.Name == roleName select r).SingleOrDefault() != null);
        }
    }
}